package org.jeecgframework.web.system.controller.core;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSPayment;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 支付方式维护
 * 
 * @author 钟杰宇
 * 
 */
@Scope("prototype")
@Controller
@RequestMapping("/paymentController")
public class PaymentController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(PaymentController.class);
	private SystemService systemService;
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Autowired
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	/**
	 * 支付方式列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "payment")
	public ModelAndView payment() {
		return new ModelAndView("system/payment/paymentList");
	}

	/**
	 * easyuiAjax表单请求
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 */
	@RequestMapping(params = "datagrid")
	public void datagrid(HttpServletRequest request,
			HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(TSPayment.class, dataGrid);
		this.systemService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除配置信息
	 * 
	 * @param payment
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(TSPayment payment, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		payment = systemService.getEntity(TSPayment.class, payment.getId());
		message = "支付方式: " + payment.getTitle() + "被删除 成功";
		systemService.delete(payment);
		systemService.addLog(message, Globals.Log_Type_DEL,
				Globals.Log_Leavel_INFO);
		
		return j;
	}

	/**
	 * 添加和更新配置信息
	 * 
	 * @param payment
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(TSPayment payment,HttpServletRequest request) {

		try {

			if (StringUtil.isEmpty(payment.getId())) {
				TSPayment pament2=systemService.findUniqueByProperty(TSPayment.class, "title", payment.getTitle());
				if(pament2!=null){
					message = "支付方式为: " + payment.getTitle() + "的信息已存在";
				}else{

					systemService.save(payment);
					message = "支付方式: " + payment.getTitle() + "被添加成功";
					systemService.addLog(message, Globals.Log_Type_INSERT,
							Globals.Log_Leavel_INFO);
				}

			}else{
				message = "支付方式: " + payment.getTitle() + "被修改成功";
				systemService.updateEntitie(payment);
				systemService.addLog(message, Globals.Log_Type_INSERT,
						Globals.Log_Leavel_INFO);
			}

		}catch (Exception e){
			e.printStackTrace();
			message = "支付方式设置失败！请检查数据是否完整！";
		}


		AjaxJson j = new AjaxJson();
		j.setMsg(message);
		
		return j;
	}

	/**
	 * 添加和更新支付方式页面
	 * 
	 * @param payment
	 * @param req
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(TSPayment payment, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(payment.getId())) {
			payment = systemService.getEntity(TSPayment.class,
					payment.getId());
			req.setAttribute("payment", payment);
		}
		return new ModelAndView("system/payment/payment");
	}

}
